package com.hzya.frame.system.user.controller;

import com.alibaba.fastjson.JSON;
import com.hzya.frame.system.dictionaryshop.entity.DictionaryShopEntity;
import com.hzya.frame.system.dictionaryshop.service.IDictionaryShopService;
import com.hzya.frame.system.role.entity.RoleEntity;
import com.hzya.frame.system.role.service.IRoleService;
import com.hzya.frame.system.user.entity.UserEntity;
import com.hzya.frame.system.user.entity.UserRoleEntity;
import com.hzya.frame.system.user.service.IUserRoleService;
import com.hzya.frame.system.user.service.IUserService;
import com.hzya.frame.utils.core.action.DefaultController;
import com.hzya.frame.utils.core.entity.JsonResultEntity;
import com.hzya.frame.utils.core.page.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @description: 用户信息  controller
 * @tableName: system_user 
 * @entityName: UserEntity
 * 包含：
 * 1、分页查询(queryPaged)
 * 2、index页面(goIndexPage)
 * 3、新增页面(goAddPage)
 * 4、新增信息保存(save)
 * 5、修改页面(goEditPage)
 * 6、修改信息保存(update)
 * 7、信息查看(goShowPage)
 * 8、删除信息(delete)
 * @author: gjh
 * @history:  1.0
 */

@Controller("system_user")
@RequestMapping("/system/user/UserController")
public class UserController extends DefaultController {
    @Autowired
    protected IUserService userService;
	@Autowired
	private IDictionaryShopService dictionAryService;
	@Autowired
	private IRoleService roleService;
	@Autowired
	private IUserRoleService userRoleService;
    /** 用户信息分页信息 **/
    @RequestMapping(value = "queryPaged")
    @ResponseBody
    public PageInfo<UserEntity> queryPaged(UserEntity entity) throws Exception {
		PageInfo<UserEntity> page =null;
		try {
		   page = userService.queryPaged(entity);
		   fixDictionaryContent(page);
		} catch (Exception e) {
		    e.printStackTrace();
		}
		return page;
    }

	/** 跳转到用户信息的index页面 **/
    @RequestMapping(value = "goIndexPage", method = RequestMethod.GET)
    public ModelAndView goIndexPage() {
		ModelAndView view = new ModelAndView();
		/**这里根据实际的需要填写需要传到前台的对象**/
		//view.addObject(attributeName, attributeValue);
		/**这里是返回对应的页面路径**/
		view.setViewName("/system/user/user/list");
		return view;
    }

	/** 跳转到用户信息信息新增页面 **/
    @RequestMapping(value = "goAddPage")
    public ModelAndView goAddPage() {
		ModelAndView view = new ModelAndView();
		/**这里根据实际的需要填写需要传到前台的对象**/
		//view.addObject(attributeName, attributeValue);
		/**这里是返回对应的页面路径**/
		/** 用户的级别*/
		List<DictionaryShopEntity> userLevelList = dictionAryService.queryDictionaries("system_module", "level");
		view.addObject("userLevelList", userLevelList);
		/* 查询当前用户所在公司的角色*/
		List<RoleEntity> roleEntityList = roleService.listGetCompanyRoles();
		view.addObject("roleEntityList", roleEntityList);

		view.setViewName("/system/user/user/add");
		return view;
    }
    
    /** 用户信息新增信息保存 **/
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity save(UserEntity entity) {
		try {
		    //bindOrgAndCompanyInfo(entity);
		    //userService.save(entity);
			userService.saveUser(entity);
		    return getSuccessMessageEntity("保存成功", "save");
		} catch (Exception e) {
		    logger.error("save", e);
		    e.printStackTrace();
		    return getFailureMessageEntity("保存失败！"+e.getMessage(), "save");
		}
    }
    
    /** 跳转到用户信息信息修改页面 **/
    @RequestMapping(value = "edit/{id}")
    public ModelAndView goEditPage(@PathVariable("id") Long id) {
		ModelAndView view = new ModelAndView();
		/**这里根据实际的需要填写需要传到前台的对象**/
		UserEntity entity = userService.get(id);
		view.addObject("entity", entity);
		/* 允许修改用户级别**/
		List<DictionaryShopEntity> userLevelList = dictionAryService.queryDictionaries("system_module", "level");
		view.addObject("userLevelList", userLevelList);
		/* 查询当前用户所在公司的角色*/
		List<RoleEntity> roleEntityList = roleService.listGetCompanyRoles();
		view.addObject("roleEntityList", roleEntityList);
		/* 查询当前用户所拥有的角色id**/
		UserRoleEntity userRoleEntity = new UserRoleEntity();
		userRoleEntity.setUser_id(id);
		List<UserRoleEntity> userRoleEntityList = userRoleService.query(userRoleEntity);
		view.addObject("userRoleEntityList", userRoleEntityList);
		/** FORM直接set值所用*/
		view.addObject("editEntity", JSON.toJSONString(entity));
		/**这里是返回对应的页面路径**/
		view.setViewName("/system/user/user/edit");
		return view;
    }
    
    /** 用户信息修改信息保存 **/
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity update(UserEntity entity) {
		try {
		    userService.updateUser(entity);
		    return getSuccessMessageEntity("更新成功！", "update");
		} catch (Exception e) {
		    logger.error("update", e);
		    e.printStackTrace();
		    return getFailureMessageEntity("更新失败！"+e.getMessage(), "update");
		}
    }
    
    /** 跳转到用户信息信息查看 **/
    @RequestMapping(value = "show/{id}")
    public ModelAndView goShowPage(@PathVariable("id") Long id) {
		ModelAndView view = new ModelAndView();
		/**这里根据实际的需要填写需要传到前台的对象**/
		UserEntity entity = userService.get(id);
		view.addObject("entity", entity);
		/* 允许修改用户级别**/
		List<DictionaryShopEntity> userLevelList = dictionAryService.queryDictionaries("system_module", "level");
		view.addObject("userLevelList", userLevelList);
		/* 查询当前用户所在公司的角色*/
		List<RoleEntity> roleEntityList = roleService.listGetCompanyRoles();
		view.addObject("roleEntityList", roleEntityList);
		/* 查询当前用户所拥有的角色id**/
		UserRoleEntity userRoleEntity = new UserRoleEntity();
		userRoleEntity.setUser_id(id);
		List<UserRoleEntity> userRoleEntityList = userRoleService.query(userRoleEntity);
		view.addObject("userRoleEntityList", userRoleEntityList);

		/** FORM直接set值所用*/
		view.addObject("showEntity", JSON.toJSONString(entity));
		/**这里是返回对应的页面路径**/
		view.setViewName("/system/user/user/show");
		return view;
    }

	/** 删除用户信息信息 **/
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity delete(Long id) {
		try {
		    userService.logicRemove(id);
		    return getSuccessMessageEntity("删除成功！", "delete");
		} catch (Exception e) {
		    logger.error("delete", e);
		    e.printStackTrace();
		    return getFailureMessageEntity("删除失败！"+e.getMessage(), "delete");
		}
    }
}

